clear

********************************************************************************
use "./data/LP_panel"

* Drop Venezuela, US, Brazil, Mexico, Taiwan, Greece
keep if ccode < 49 
drop if ccode == 5 | ccode == 28 | ccode == 45 | ccode == 15 


drop mps mpu
rename mps_2day mps
rename mpu_2day mpu


* Crisis dummy
gen crisis = 0
replace crisis = 1 if date >= td(01dec2007) & date < td(01jul2009)

***** variables to store the impulse response (vector of betas from the LP regressions) and standard errors
gen adv_2y_mpu=.
gen adv_std_2y_mpu=.

gen eme_2y_mpu=.
gen eme_std_2y_mpu=.

gen adv_10y_mpu=.
gen adv_std_10y_mpu=.

gen eme_10y_mpu=.
gen eme_std_10y_mpu=.

gen adv_stk_mpu=.
gen adv_std_stk_mpu=.

gen eme_stk_mpu=.
gen eme_std_stk_mpu=.

* number of lags
local MaxLPLags 0

* horizon
local horizon 45

*************************** LP regressions *************************************
* Reset tsset
egen time2 = group(date)
sort ccode time2
tsset ccode time2

forvalues i=1/`horizon' {

	* 2-year Adv
	xtscc scl_two_y`i' mps mpu if advanced == 1, lag(3) 
	eststo ols_3m`i'
	
	replace adv_2y_mpu  = _b[mpu] if _n == `i'+1
	replace adv_std_2y_mpu = _se[mpu] if _n == `i'+1	
	
	eststo clear
	
}


forvalues i=1/`horizon' {

	* 2-year Eme
	xtscc scl_two_y`i' mps mpu if advanced == 0, lag(3) 
	eststo ols_3m`i'		
	
	replace eme_2y_mpu  = _b[mpu] if _n == `i'+1
	replace eme_std_2y_mpu = _se[mpu] if _n == `i'+1			
	
	eststo clear
}


forvalues i=1/`horizon' {

	* 10-year Adv
	xtscc scl_ten_y`i' mps mpu if advanced == 1, lag(3) 
	eststo ols_3m`i'
	
	replace adv_10y_mpu  = _b[mpu] if _n == `i'+1
	replace adv_std_10y_mpu = _se[mpu] if _n == `i'+1	
	
	eststo clear
	
}


forvalues i=1/`horizon' {

	* 10-year Eme
	xtscc scl_ten_y`i' mps mpu if advanced == 0, lag(3) 
	eststo ols_3m`i'

	replace eme_10y_mpu  = _b[mpu] if _n == `i'+1
	replace eme_std_10y_mpu = _se[mpu] if _n == `i'+1			
	
	eststo clear
}


forvalues i=1/`horizon' {

	* Stock Adv
	xtscc scl_stock`i' mps mpu if advanced == 1, lag(3) 
	eststo ols_3m`i'	
	
	replace adv_stk_mpu  = _b[mpu] if _n == `i'+1
	replace adv_std_stk_mpu = _se[mpu] if _n == `i'+1	
	
	eststo clear
	
}


forvalues i=1/`horizon' {

	* Stock Eme
	xtscc scl_stock`i' mps mpu if advanced == 0, lag(3) 
	eststo ols_3m`i'		
	
	replace eme_stk_mpu  = _b[mpu] if _n == `i'+1
	replace eme_std_stk_mpu = _se[mpu] if _n == `i'+1			
	
	eststo clear
}


****************************************************************
*** Baseline OLS LP graphs
****************************************************************

gen Days = _n-1 if _n <= `horizon' +1
replace Days = . if Days == 0

* zero line
gen zero = 0 if _n <= `horizon' +1		

***** create confidence bands (in this case 68 and 95%) ****
	scalar sig1 = 0.05	 
	scalar sig2 = 0.32	 
	

	gen up_2y_adv = adv_2y_mpu + invnormal(1-sig1/2)*adv_std_2y_mpu if _n <= (`horizon' + 1)
	gen dn_2y_adv = adv_2y_mpu - invnormal(1-sig1/2)*adv_std_2y_mpu if _n <= (`horizon' + 1)

	gen up2_2y_adv = adv_2y_mpu + invnormal(1-sig2/2)*adv_std_2y_mpu if _n <= (`horizon' + 1)
	gen dn2_2y_adv = adv_2y_mpu - invnormal(1-sig2/2)*adv_std_2y_mpu if _n <= (`horizon' + 1)
	
	gen up_2y_eme = eme_2y_mpu + invnormal(1-sig1/2)*eme_std_2y_mpu if _n <= (`horizon' + 1)
	gen dn_2y_eme = eme_2y_mpu - invnormal(1-sig1/2)*eme_std_2y_mpu if _n <= (`horizon' + 1)

	gen up2_2y_eme = eme_2y_mpu + invnormal(1-sig2/2)*eme_std_2y_mpu if _n <= (`horizon' + 1)
	gen dn2_2y_eme = eme_2y_mpu - invnormal(1-sig2/2)*eme_std_2y_mpu if _n <= (`horizon' + 1)
	
	
	gen up_10y_adv = adv_10y_mpu + invnormal(1-sig1/2)*adv_std_10y_mpu if _n <= (`horizon' + 1)
	gen dn_10y_adv = adv_10y_mpu - invnormal(1-sig1/2)*adv_std_10y_mpu if _n <= (`horizon' + 1)

	gen up2_10y_adv = adv_10y_mpu + invnormal(1-sig2/2)*adv_std_10y_mpu if _n <= (`horizon' + 1)
	gen dn2_10y_adv = adv_10y_mpu - invnormal(1-sig2/2)*adv_std_10y_mpu if _n <= (`horizon' + 1)
	
	gen up_10y_eme = eme_10y_mpu + invnormal(1-sig1/2)*eme_std_10y_mpu if _n <= (`horizon' + 1)
	gen dn_10y_eme = eme_10y_mpu - invnormal(1-sig1/2)*eme_std_10y_mpu if _n <= (`horizon' + 1)

	gen up2_10y_eme = eme_10y_mpu + invnormal(1-sig2/2)*eme_std_10y_mpu if _n <= (`horizon' + 1)
	gen dn2_10y_eme = eme_10y_mpu - invnormal(1-sig2/2)*eme_std_10y_mpu if _n <= (`horizon' + 1)
	
	
	gen up_stk_adv = adv_stk_mpu + invnormal(1-sig1/2)*adv_std_stk_mpu if _n <= (`horizon' + 1)
	gen dn_stk_adv = adv_stk_mpu - invnormal(1-sig1/2)*adv_std_stk_mpu if _n <= (`horizon' + 1)

	gen up2_stk_adv = adv_stk_mpu + invnormal(1-sig2/2)*adv_std_stk_mpu if _n <= (`horizon' + 1)
	gen dn2_stk_adv = adv_stk_mpu - invnormal(1-sig2/2)*adv_std_stk_mpu if _n <= (`horizon' + 1)
	
	gen up_stk_eme = eme_stk_mpu + invnormal(1-sig1/2)*eme_std_stk_mpu if _n <= (`horizon' + 1)
	gen dn_stk_eme = eme_stk_mpu - invnormal(1-sig1/2)*eme_std_stk_mpu if _n <= (`horizon' + 1)

	gen up2_stk_eme = eme_stk_mpu + invnormal(1-sig2/2)*eme_std_stk_mpu if _n <= (`horizon' + 1)
	gen dn2_stk_eme = eme_stk_mpu - invnormal(1-sig2/2)*eme_std_stk_mpu if _n <= (`horizon' + 1)
	
	
************************ IRF Graphs ********************************************

*** MPU ***

local two_y_adv "Advanced: 2 Year Response"
local two_y_eme "Emerging: 2 Year Response"
local ten_y_adv "Advanced: 10 Year Response"
local ten_y_eme "Emerging: 10 Year Response"
local stk_adv "Advanced: Equity Response"
local stk_eme "Emerging: Equity Response"

grstyle init
grstyle set plain, horizontal grid noextend compact dotted

* 2 Year
	twoway (rarea up_2y_adv dn_2y_adv  Days, ///
	fcolor(gs12) lcolor(white) lpattern(solid)) ///
	(rarea up2_2y_adv dn2_2y_adv  Days, ///
	fcolor(gs10) lcolor(white) lpattern(solid)) ///
	(line adv_2y_mpu Days, lcolor(blue) ///
	lpattern(solid) lwidth(medium)) ///
	(line zero Days, lcolor(black)), ///
	xtitle("Days", size(vsmall)) ///
	xscale(range(1 45)) ///
	xlabel(0 "0" 15 "15" 30 "30" 45 "45") ///
	title("`two_y_adv'", color(black) size(small)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(twoyearadv, replace) 
	graph save   two_y_adv.gph, replace
	
	twoway (rarea up_2y_eme dn_2y_eme  Days, ///
	fcolor(gs12) lcolor(white) lpattern(solid)) ///
	(rarea up2_2y_eme dn2_2y_eme  Days, ///
	fcolor(gs10) lcolor(white) lpattern(solid)) ///
	(line eme_2y_mpu Days, lcolor(red) ///
	lpattern(solid) lwidth(medium)) ///
	(line zero Days, lcolor(black)), ///
	xtitle("Days", size(vsmall)) ///
	xscale(range(1 45)) ///
	xlabel(0 "0" 15 "15" 30 "30" 45 "45") ///
	title("`two_y_eme'", color(black) size(small)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(twoyeareme, replace) 
	graph save   two_y_eme.gph, replace
	
	graph combine two_y_adv.gph two_y_eme.gph, col(2) ycommon name(twoyear_mpu_2day, replace) 
	graph save 2yr_LP.gph, replace
	
* 10 Year	
	twoway (rarea up_10y_adv dn_10y_adv  Days, ///
	fcolor(gs12) lcolor(white) lpattern(solid)) ///
	(rarea up2_10y_adv dn2_10y_adv  Days, ///
	fcolor(gs10) lcolor(white) lpattern(solid)) ///
	(line adv_10y_mpu Days, lcolor(blue) ///
	lpattern(solid) lwidth(medium)) ///
	(line zero Days, lcolor(black)), ///
	xtitle("Days", size(vsmall)) ///
	xscale(range(1 45)) ///
	xlabel(0 "0" 15 "15" 30 "30" 45 "45") ///
	title("`ten_y_adv'", color(black) size(small)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(tenyearadv, replace) 
	graph save   ten_y_adv.gph, replace
	
	twoway (rarea up_10y_eme dn_10y_eme  Days, ///
	fcolor(gs12) lcolor(white) lpattern(solid)) ///
	(rarea up2_10y_eme dn2_10y_eme  Days, ///
	fcolor(gs10) lcolor(white) lpattern(solid)) ///
	(line eme_10y_mpu Days, lcolor(red) ///
	lpattern(solid) lwidth(medium)) ///
	(line zero Days, lcolor(black)), ///
	xtitle("Days", size(vsmall)) ///
	xscale(range(1 45)) ///
	xlabel(0 "0" 15 "15" 30 "30" 45 "45") ///
	title("`ten_y_eme'", color(black) size(small)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(tenyeareme, replace) 
	graph save   ten_y_eme.gph, replace
	
	graph combine ten_y_adv.gph ten_y_eme.gph, col(2) ycommon name(tenyear_mpu_2day, replace) 
	graph save 10yr_LP.gph, replace

* Stocks
	twoway (rarea up_stk_adv dn_stk_adv  Days, ///
	fcolor(gs12) lcolor(white) lpattern(solid)) ///
	(rarea up2_stk_adv dn2_stk_adv  Days, ///
	fcolor(gs10) lcolor(white) lpattern(solid)) ///
	(line adv_stk_mpu Days, lcolor(blue) ///
	lpattern(solid) lwidth(medium)) ///
	(line zero Days, lcolor(black)), ///
	xtitle("Days", size(vsmall)) ///
	xscale(range(1 45)) ///
	xlabel(0 "0" 15 "15" 30 "30" 45 "45") ///
	title("`stk_adv'", color(black) size(small)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(stkadv, replace) 
	graph save   stk_adv.gph, replace
	
	twoway (rarea up_stk_eme dn_stk_eme  Days, ///
	fcolor(gs12) lcolor(white) lpattern(solid)) ///
	(rarea up2_stk_eme dn2_stk_eme  Days, ///
	fcolor(gs10) lcolor(white) lpattern(solid)) ///
	(line eme_stk_mpu Days, lcolor(red) ///
	lpattern(solid) lwidth(medium)) ///
	(line zero Days, lcolor(black)), ///
	xtitle("Days", size(vsmall)) ///
	xscale(range(1 45)) ///
	xlabel(0 "0" 15 "15" 30 "30" 45 "45") ///
	title("`stk_eme'", color(black) size(small)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(stkeme, replace) 
	graph save   stk_eme.gph, replace
	
	graph combine stk_adv.gph stk_eme.gph, col(2) ycommon name(stock_mpu_2day, replace) 
	graph save stk_LP.gph, replace
	

	*Combine graphs
	graph combine two_y_adv.gph two_y_eme.gph ten_y_adv.gph ten_y_eme.gph stk_adv.gph stk_eme.gph , rows(3) col(2) name(all_mpu_2day, replace) 
	graph save all_LP.gph, replace
	
********************************************************************************
